package net.zdsoft.wpchat.support.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import net.zdsoft.keel.jdbc.JdbcBasicDao;
import net.zdsoft.keel.jdbc.MapRowMapper;
import net.zdsoft.keel.jdbc.MultiRowMapper;
import net.zdsoft.keel.jdbc.SingleRowMapper;
import net.zdsoft.keel.util.Validators;
import net.zdsoft.wpchat.support.dao.SysOptionDao;
import net.zdsoft.wpchat.support.entity.SysOption;

/**
 * 系统参数实现
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2014-9-22 下午10:43:01 $
 */
@Repository
public class SysOptionDaoImpl extends JdbcBasicDao implements SysOptionDao {
    private static final String SQL_FIND_SYSOPTIONS = "SELECT id,value FROM sys_option";
    private static final String SQL_FIND_NAME_2_VALUE_MAP = "SELECT name,value FROM sys_option";
    private static final String SQL_FIND_ALL_SYSOPTIONS = "SELECT * FROM sys_option";
    private static final String SQL_DELETE_BY_OPERATOR_ID_IN = "DELETE FROM sys_option WHERE id = ?";
    private static final String SQL_FIND_SYSOPTION = "SELECT value FROM sys_option WHERE id = ?";
    private static final String SQL_UPDATE_SYSOPTION = "UPDATE sys_option set value=?, name=?, decription=? where id=?";
    private static final String SQL_FIND_OPTION_BY_ID = "SELECT * FROM sys_option WHERE id = ?";
    private static final String SQL_INSERT_OPERATOR = "INSERT INTO sys_option(id, value ,name,decription) VALUES(?, ?, ?, ?)";
    private static final String SQL_FIND_SYSOPTIONLIST_BY_NAMES = "SELECT * FROM sys_option WHERE name IN ";

    @Override
    public Map<String, String> findSysOptions() {
        return queryForMap(SQL_FIND_SYSOPTIONS, new MapRowMapper<String, String>() {
            @Override
            public String mapRowKey(ResultSet rs, int rowNum) throws SQLException {
                return rs.getString("id");
            }

            @Override
            public String mapRowValue(ResultSet rs, int rowNum) throws SQLException {
                return rs.getString("value");
            }
        });
    }

    @Override
    public Map<String, String> findName2ValueMap() {
        return queryForMap(SQL_FIND_NAME_2_VALUE_MAP, new MapRowMapper<String, String>() {
            @Override
            public String mapRowKey(ResultSet rs, int n) throws SQLException {
                return rs.getString("name");
            }

            @Override
            public String mapRowValue(ResultSet rs, int n) throws SQLException {
                return rs.getString("value");
            }
        });
    }

    @Override
    public List<SysOption> findAllSysOptions() {
        return query(SQL_FIND_ALL_SYSOPTIONS, new MMultiRowMapper());
    }

    @Override
    public void updateSysOption(SysOption sysOption) {
        if (null == sysOption) {
            return;
        }
        List<Object[]> listOfArgs = new ArrayList<Object[]>();
        listOfArgs.add(new Object[] { sysOption.getValue(), sysOption.getName(), sysOption.getDecription(),
                sysOption.getId() });
        batchUpdate(SQL_UPDATE_SYSOPTION, listOfArgs, new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                Types.CHAR });
    }

    @Override
    public void deleteSysOptionByIds(String id) {
        update(SQL_DELETE_BY_OPERATOR_ID_IN, new Object[] { id });
    }

    @Override
    public SysOption findSysOptionById(String id) {
        return query(SQL_FIND_OPTION_BY_ID, new Object[] { id }, new SingleRowMapper<SysOption>() {
            @Override
            public SysOption mapRow(ResultSet rs) throws SQLException {
                return new MMultiRowMapper().mapRow(rs, 1);
            }
        });

    }

    @Override
    public void insertSysOption(SysOption sysOption) {
        if (sysOption == null) {
            return;
        }

        update(SQL_INSERT_OPERATOR, new Object[] { sysOption.getId(), sysOption.getValue(), sysOption.getName(),
                sysOption.getDecription() });
    }

    @Override
    public String findSysOptionValue(String id) {
        return queryForNotNullString(SQL_FIND_SYSOPTION, new Object[] { id });
    }

    @Override
    public List<SysOption> findSysOptionListByNames(String... names) {
        if (Validators.isEmpty(names)) {
            return Collections.emptyList();
        }

        return queryForInSQL(SQL_FIND_SYSOPTIONLIST_BY_NAMES, null, names, new MMultiRowMapper());
    }

    /**
     * 多条结果集
     * 
     * @author xuan
     * @version $Revision: 1.0 $, $Date: 2014-8-19 下午1:28:00 $
     */
    private static class MMultiRowMapper implements MultiRowMapper<SysOption> {
        @Override
        public SysOption mapRow(ResultSet rs, int num) throws SQLException {
            SysOption sysOption = new SysOption();
            sysOption.setId(rs.getString("id"));
            sysOption.setName(rs.getString("name"));
            sysOption.setValue(rs.getString("value"));
            sysOption.setDecription(rs.getString("decription"));
            return sysOption;
        }
    }

}
